package com.example.lastwork.entity;

import com.example.lastwork.common.entity.LogicEntity;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import java.util.Set;

@Entity
@Getter
@Setter
@Where(clause = "is_deleted = 0")
public class Shop extends LogicEntity {
    @Column
    private String shopName;

    @JsonIgnore
    @ManyToOne(fetch = FetchType.LAZY)
    @JsonIgnoreProperties("shop")
    private Business business;

    @JsonIgnore
    @OneToMany(mappedBy = "shop",
            fetch = FetchType.LAZY,
            cascade = CascadeType.ALL)
    @JsonIgnoreProperties("shop")
    @JsonBackReference
    private Set<ShopItem> shopItems;

    @JsonIgnore
    @OneToMany(mappedBy = "shop",
            fetch = FetchType.LAZY,
            cascade = CascadeType.ALL)
    @JsonIgnoreProperties("shop")
    @JsonBackReference
    private Set<Orders> orders;

    public ShopItem onSale(String shopItemName, Double price){
        ShopItem shopItem = new ShopItem();
        shopItem.setShopItemName(shopItemName);
        shopItem.setPrice(price);
        shopItem.setShop(this);
        shopItems.add(shopItem);
        return shopItem;
    }
}
